{% extends "base.html" %} 

{% block title %}用户购买页面{% endblock %} 



{% block content %}
<div>
    <h1>用户购买页面</h1>
    <form action="">
        <p>
            <select name="uid">
                <option value="0">请选择购物用户</option>
                {% for user in users %}
                    <option value="{{ user.id }}">{{ user.username }}</option>
                {% endfor %}
            </select>
        </p>

        <p>
            <table border="1" cellspacing="0" width="50%">
                <tr>
                    <th>序号</th>
                    <th>商品名</th>
                    <th>价格</th>
                    <th>操作</th>
                </tr>
                
                {% for goods in goods_list %}
                    <tr>
                        <td class="table-tag-center">{{ loop.index }}</td>
                        <td class="table-tag-center">
                            <!-- 商品名称加一个连接，根据商品名找那些用户购买了该商品 -->
                            <a href="{{ url_for('goods.find_user') }}?gid={{goods.id}}">
                                {{ goods.gname }}
                            </a>
                        </td>
                        <td class="table-tag-center">{{ goods.price }}</td>
                        <td class="table-tag-center">
                            <!-- button按钮会和form的表单事件冲突，所以需要阻止默认事件 -->
                            <input type="button" class="btnbuy" tag="{{ goods.id}}"
                            style="background:red" value="购买">
                        </td> 
                    </tr>       
                {% endfor %}
            </table>
        </p>
    </form>
</div>
{% endblock %}

{% block head %}
    <style>
    .table-tag-center {
            text-align: center;
        }
    </style>

    <script>
        $(document).ready(function(){
            // 购买的脚本
            $(".btnbuy").click(function(){
                var user_id = $("select[name='uid']").val();
                var goods_id = $(this).attr("tag"); // 购买标签的tag属性
                location.href = "{{ url_for('goods.buy') }}?uid=" + user_id + "&gid=" + goods_id;
            });
        });
        
    </script>
   
{% endblock %}